from sqlalchemy import Column, Integer, String, DateTime, ForeignKey, Enum, Text, Date
from sqlalchemy.orm import relationship
from sqlalchemy.sql import func
from app.core.database import Base
import enum

class ProjectStatus(str, enum.Enum):
    PENDING_CONTRACT = "pending_contract"
    PENDING_START = "pending_start"
    IN_PROGRESS = "in_progress"
    ACCEPTED = "accepted"
    SETTLED = "settled"

class Project(Base):
    __tablename__ = "projects"
    
    id = Column(Integer, primary_key=True, index=True)
    customer_id = Column(Integer, ForeignKey("customers.id"), nullable=False, index=True)
    contract_id = Column(Integer, ForeignKey("contracts.id"), nullable=True, index=True)
    project_number = Column(String(50), unique=True, nullable=False, index=True)
    room_number = Column(String(50), nullable=False, index=True)
    apartment_type = Column(String(10), nullable=False, index=True)
    status = Column(Enum(ProjectStatus), default=ProjectStatus.PENDING_CONTRACT, nullable=False, index=True)
    current_stage = Column(String(50), nullable=True)
    start_date = Column(Date, nullable=True)
    completion_date = Column(Date, nullable=True)
    notes = Column(Text, nullable=True)
    created_at = Column(DateTime(timezone=True), server_default=func.now())
    updated_at = Column(DateTime(timezone=True), onupdate=func.now())
    
    customer = relationship("Customer", back_populates="projects")
    contract = relationship("Contract", back_populates="project")
    stages = relationship("ProjectStage", back_populates="project", cascade="all, delete-orphan")
    files = relationship("ProjectFile", back_populates="project", cascade="all, delete-orphan")
    change_orders = relationship("ChangeOrder", back_populates="project", cascade="all, delete-orphan")

